package com.softaria.spkiller.mojo;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.maven.plugin.MojoExecutionException;

import com.softaria.spkiller.analyzer.dependency.DependencyProblemCollector;
import com.softaria.spkiller.layers.LayerIdentifier;
import com.softaria.spkiller.metadata.ClassIdentifier;

public class MojoDependencyProblemCollector implements
		DependencyProblemCollector {
	
	private class ProhibitedDependency {
		
		private ClassIdentifier classID;
		private LayerIdentifier layer;
		private ClassIdentifier usedClassId;
		private LayerIdentifier usedClassLayer;
		
		public ProhibitedDependency(ClassIdentifier classID,
				LayerIdentifier layer, ClassIdentifier usedClassId,
				LayerIdentifier usedClassLayer) {
			super();
			this.classID = classID;
			this.layer = layer;
			this.usedClassId = usedClassId;
			this.usedClassLayer = usedClassLayer;
		}

		@Override
		public String toString() {
			return "Prohibited Dependency from [" + classID + "] belonging to layer="
					+ layer + " to [" + usedClassId
					+ "] belonging to layer " + usedClassLayer;
		}
		
		
	}
	
	private Collection<ClassIdentifier> noLayers = new ArrayList<ClassIdentifier>();
	private Collection<ProhibitedDependency> prohibitedDependencies = new ArrayList<MojoDependencyProblemCollector.ProhibitedDependency>();

	public void reportNoLayer(ClassIdentifier classID) {
		noLayers .add(classID);

	}

	public void reportProhibitedDependency(ClassIdentifier classID,
			LayerIdentifier layer, ClassIdentifier usedClassId,
			LayerIdentifier usedClassLayer) {
		
		prohibitedDependencies .add(new ProhibitedDependency(classID, layer, usedClassId, usedClassLayer));

	}
	
	public void check() throws MojoExecutionException {
		for(ClassIdentifier id:noLayers) {
			System.out.println("SPAGHETTI-KILLER ERROR: No layer found for class: "+id);
		}
		
		for(ProhibitedDependency problem:prohibitedDependencies) {
			System.out.println("SPAGHETTI-KILLER ERROR: "+problem.toString()  );
		}
		
		if(!noLayers.isEmpty()||!prohibitedDependencies.isEmpty()) {
			throw new MojoExecutionException("See above");
		}

	}

}
